var vue = new Vue({
    el: "#main",
    data() {
        return {
            total: 0, //总数
            monitorCommunityList: [], //列表
            id: "", //展开数据的id
            searchVal: '',
            searchLoading: false,
            week: '', //关爱的第几周
            year: '', //年份
            yearArr: [], //年份数组
        }
    },
    created() {
        this.year = getRequest().year
        console.log(this.year)
        this.week = getRequest().week
        this.getTime()
    },
    mounted() {
        this.getCommunityList()
    },
    methods: {
        // 获取时间
        getTime() {
            let time = new Date()
            // this.year = time.getFullYear()
            // let firstDayOfYear = new Date(time.getFullYear(), 0, 1)
            // const dayOfWeek = firstDayOfYear.getDay()
            // let spendDay = 1;
            // if (dayOfWeek != 0) {
            //     spendDay = 7 - dayOfWeek + 1;
            // }
            // firstDayOfYear = new Date(time.getFullYear(), 0, spendDay);
            // const d = Math.ceil((time.valueOf() - firstDayOfYear.valueOf()) / 86400000);
            // 获取当前周数
            // this.week = Math.ceil(d / 7);

            // let currentMonth = time.getMonth() + 1;
            // if (currentMonth >= 1 && currentMonth <= 3) {
            //     this.rateIndex = 0;
            // } else if (currentMonth >= 4 && currentMonth <= 6) {
            //     this.rateIndex = 1;
            // } else if (currentMonth >= 7 && currentMonth <= 9) {
            //     this.rateIndex = 2;
            // } else {
            //     this.rateIndex = 3;
            // }
            let _year = time.getFullYear() - 2023
            for (let i = 0; i <= _year; i++) {
                this.yearArr.push(time.getFullYear() - i)
            }
        },
        // 改变社区关爱走访率年
        changeYear(item) {
            console.log(item)
            let data = {
                year: '',
                week: '',
                dept: [{
                    dept_id: item.dept_id,
                    year: item.year,
                    week: item.week,
                }],
                user: [{
                    user_id: '',
                    year: '',
                    week: '',
                }],

            }
            ajax({
                url: formal + '/sick-man/get-grids?keywords=' + this.searchVal,
                type: 'POST',
                data: data,
                success: (res) => {
                    let arr = res.data.list.filter((e) => e.dept_id == item.dept_id)[0]
                    item.user_list = arr.user_list
                }
            })

        },
        // 改变社区关爱走访率周
        changeRate(item, val) {
            if (val == -1) {
                if (item.week == 1) {
                    vant.Toast('已经是本年的第一周');
                    return
                }
            } else {
                if (item.week == 52) {
                    vant.Toast('已经是本年的最后一周');
                    return
                }
            }
            item.week = item.week * 1 + val
            let data = {
                year: '',
                week: '',
                dept: [{
                    dept_id: item.dept_id,
                    year: item.year,
                    week: item.week,
                }],
                user: [{
                    user_id: '',
                    year: '',
                    week: '',
                }],

            }
            ajax({
                url: formal + '/sick-man/get-grids?keywords=' + this.searchVal,
                type: 'POST',
                data: data,
                success: (res) => {
                    let arr = res.data.list.filter((e) => e.dept_id == item.dept_id)[0]
                    console.log(arr)
                    item.user_list = arr.user_list
                    item.care_rate = arr.care_rate
                }
            })

        },
        // 改变社区关爱责任人走访率年
        changeYearPerson(e, item, dept_id) {
            let year = e.target.value
            item.year = year
            let data = {
                year: '',
                week: '',
                dept: {
                    dept_id: '',
                    year: '',
                    week: '',
                },
                user: [{
                    user_id: item.user_id,
                    year: item.year,
                    week: item.week,
                }],

            }
            ajax({
                url: formal + '/sick-man/get-grids?keywords=' + this.searchVal,
                type: 'POST',
                data: data,
                success: (res) => {
                    let arr = res.data.list.filter((e) => e.dept_id == dept_id)[0]
                    arr = arr.user_list.filter((e) => e.user_id == item.user_id)[0]
                    item.care_rate = arr.care_rate

                }
            })

        },
        // 改变社区关爱责任人走访率周
        changeRatePerson(item, dept_id, val) {
            console.log(item)
            if (val == -1) {
                if (item.week == 1) {
                    vant.Toast('已经是本年的第一周');
                    return
                }
            } else {
                if (item.week == 52) {
                    vant.Toast('已经是本年的最后一周');
                    return
                }
            }
            item.week = item.week * 1 + val
            let data = {
                year: '',
                week: '',
                dept: [{
                    dept_id: '',
                    year: '',
                    week: '',
                }],
                user: [{
                    user_id: item.user_id,
                    year: item.year,
                    week: item.week,
                }],

            }
            ajax({
                url: formal + '/sick-man/get-grids?keywords=' + this.searchVal,
                type: 'POST',
                data: data,
                success: (res) => {
                    console.log(res)
                    let arr = res.data.list.filter((e) => e.dept_id == dept_id)[0]
                    arr = arr.user_list.filter((e) => e.user_id == item.user_id)[0]

                    item.care_rate = arr.care_rate

                }
            })
        },
        // 获取监控社区列表
        getCommunityList() {
            let data = {
                year: this.year,
                week: this.week,
                dept: {
                    dept_id: '',
                    year: '',
                    week: '',
                },
                user: {
                    user_id: '',
                    year: '',
                    week: '',
                },

            }
            ajax({
                url: formal + '/sick-man/get-grids?keywords=' + this.searchVal,
                type: 'POST',
                data: data,
                success: (res) => {
                    this.searchLoading = false
                    this.total = res.data.man_total
                    this.monitorCommunityList = res.data.list
                }
            })
        },
        searchChange() {
            if (this.searchVal == '') {
                this.getCommunityList()
            }
        },
        search() {
            if (this.searchVal == '') return;
            this.searchLoading = true
            this.getCommunityList()
        },
        // 前往网格关爱列表
        handleMonitorMarker(id, user_id) {
            location.href = '../seekPeopleList.html?id=' + id + '&user_id=' + user_id
        }

    }
})